<?php

namespace yifang\model\concern;

use Closure;
use yifang\model\Model;
use yifang\utils\Str;
use yifang\db\BaseQuery as Query;
use yifang\model\Relation;
use yifang\model\relation\OneToOne;
use yifang\model\relation\HasOne;
use yifang\model\relation\HasOneThrough;
use yifang\model\relation\BelongsTo;
use yifang\model\relation\BelongsToMany;
use yifang\model\relation\HasMany;
use yifang\model\relation\HasManyThrough;
use yifang\model\relation\MorphMany;
use yifang\model\relation\MorphOne;
use yifang\model\relation\MorphTo;
use yifang\model\relation\MorphToMany;

trait RelationShip
{
    /**
     * 父关联模型对象
     * @var object
     */
    public $parent;

    /**
     * 模型关联数据
     * @var array
     */
    private $relation = [];

    /**
     * 关联写入定义信息
     * @var array
     */
    private $together = [];

    /**
     * 关联自动写入信息
     * @var array
     */
    protected $relationWrite = [];

    /**
     * 设置父关联对象
     * @param  Model $model  模型对象
     * @return $this
     */
    public function setParent(Model $model)
    {
        $this->parent = $model;
        return $this;
    }

    /**
     * 获取父关联对象
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * 获取当前模型的关联模型数据
     * @return mixed
     */
    public function getRelation($name = null, $auto = false)
    {
        if (is_null($name)) {
            return $this->relation;
        }
        if (array_key_exists($name, $this->relation)) {
            return $this->relation[$name];
        } elseif ($auto) {
            $relation = Str::camel($name);
            return $this->getRelationValue($relation);
        }
    }

    /**
     * 设置关联数据对象值
     * @return $this
     */
    public function setRelation($name, $value, array $data = [])
    {
        // 检测修改器
        $method = 'set' . Str::studly($name) . 'Attr';
        if (method_exists($this, $method)) {
            $value = $this->$method($value, array_merge($this->data, $data));
        }
        $this->relation[$this->getRealFieldName($name)] = $value;
        return $this;
    }

    /**
     * 查询当前模型的关联数据
     * @return void
     */
    public function relationQuery(array $relations, array $withRelationAttr = [])
    {
        foreach ($relations as $key => $relation) {
            $subRelation = [];
            $closure     = null;
            if ($relation instanceof Closure) {
                // 支持闭包查询过滤关联条件
                $closure  = $relation;
                $relation = $key;
            }
            if (is_array($relation)) {
                $subRelation = $relation;
                $relation    = $key;
            } elseif (strpos($relation, '.')) {
                list($relation, $subRelation) = explode('.', $relation, 2);
            }
            $method       = Str::camel($relation);
            $relationName = Str::snake($relation);
            $relationResult = $this->$method();
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
            $this->relation[$relation] = $relationResult->getRelation((array) $subRelation, $closure);
        }
    }

    /**
     * 关联数据写入
     * @return $this
     */
    public function together(array $relation)
    {
        $this->together = $relation;
        $this->checkAutoRelationWrite();
        return $this;
    }

    /**
     * 根据关联条件查询当前模型
     * @return Query
     */
    public static function has($relation, $operator = '>=', $count = 1, $id = '*', $joinType = '', Query $query = null)
    {
        return (new static())->$relation()->has($operator, $count, $id, $joinType, $query);
    }

    /**
     * 根据关联条件查询当前模型
     * @return Query
     */
    public static function hasWhere($relation, $where = [], $fields = '*', $joinType = '', Query $query = null)
    {
        return (new static())->$relation()->hasWhere($where, $fields, $joinType, $query);
    }

    /**
     * 预载入关联查询 JOIN方式
     * @return bool
     */
    public function eagerly(Query $query, $relation, $field, $joinType = '', Closure $closure = null, $first = false)
    {
        $relation = Str::camel($relation);
        $class    = $this->$relation();
        if ($class instanceof OneToOne) {
            $class->eagerly($query, $relation, $field, $joinType, $closure, $first);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 预载入关联查询 返回数据集
     * @return void
     */
    public function eagerlyResultSet(array &$resultSet, array $relations, array $withRelationAttr = [], $join = false, $cache = false)
    {
        foreach ($relations as $key => $relation) {
            $subRelation = [];
            $closure     = null;
            if ($relation instanceof Closure) {
                $closure  = $relation;
                $relation = $key;
            }
            if (is_array($relation)) {
                $subRelation = $relation;
                $relation    = $key;
            } elseif (strpos($relation, '.')) {
                list($relation, $subRelation) = explode('.', $relation, 2);

                $subRelation = [$subRelation];
            }
            $relationName = $relation;
            $relation     = Str::camel($relation);
            $relationResult = $this->$relation();
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
            if (is_scalar($cache)) {
                $relationCache = [$cache];
            } else {
                $relationCache = $cache[$relationName] ?? $cache;
            }
            $relationResult->eagerlyResultSet($resultSet, $relationName, $subRelation, $closure, $relationCache, $join);
        }
    }

    /**
     * 预载入关联查询 返回模型对象
     * @return void
     */
    public function eagerlyResult(array $relations, array $withRelationAttr = [], $join = false, $cache = false)
    {
        foreach ($relations as $key => $relation) {
            $subRelation = [];
            $closure     = null;
            if ($relation instanceof Closure) {
                $closure  = $relation;
                $relation = $key;
            }
            if (is_array($relation)) {
                $subRelation = $relation;
                $relation    = $key;
            } elseif (strpos($relation, '.')) {
                list($relation, $subRelation) = explode('.', $relation, 2);

                $subRelation = [$subRelation];
            }
            $relationName = $relation;
            $relation     = Str::camel($relation);
            $relationResult = $this->$relation();
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
            if (is_scalar($cache)) {
                $relationCache = [$cache];
            } else {
                $relationCache = $cache[$relationName] ?? [];
            }
            $relationResult->eagerlyResult($this, $relationName, $subRelation, $closure, $relationCache, $join);
        }
    }

    /**
     * 绑定（一对一）关联属性到当前模型
     */
    public function bindAttr($relation, array $attrs = [])
    {
        $relation = $this->getRelation($relation);
        foreach ($attrs as $key => $attr) {
            $key   = is_numeric($key) ? $attr : $key;
            $value = $this->getOrigin($key);
            if (!is_null($value)) {
                throw new \Exception('bind attr has exists:' . $key);
            }
            $this->set($key, $relation ? $relation->$attr : null);
        }
        return $this;
    }

    /**
     * 关联统计
     * @return void
     */
    public function relationCount(Query $query, array $relations, $aggregate = 'sum', $field = '*', $useSubQuery = true)
    {
        foreach ($relations as $key => $relation) {
            $closure = $name = null;
            if ($relation instanceof Closure) {
                $closure  = $relation;
                $relation = $key;
            } elseif (is_string($key)) {
                $name     = $relation;
                $relation = $key;
            }
            $relation = Str::camel($relation);
            if ($useSubQuery) {
                $count = $this->$relation()->getRelationCountQuery($closure, $aggregate, $field, $name);
            } else {
                $count = $this->$relation()->relationCount($this, $closure, $aggregate, $field, $name);
            }
            if (empty($name)) {
                $name = Str::snake($relation) . '_' . $aggregate;
            }
            if ($useSubQuery) {
                $query->field(['(' . $count . ')' => $name]);
            } else {
                $this->setAttr($name, $count);
            }
        }
    }

    /**
     * HAS ONE 关联定义
     * @return HasOne
     */
    public function hasOne($model, $foreignKey = '', $localKey = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        return new HasOne($this, $model, $foreignKey, $localKey);
    }

    /**
     * BELONGS TO 关联定义
     * @return BelongsTo
     */
    public function belongsTo($model, $foreignKey = '', $localKey = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $foreignKey = $foreignKey ?: $this->getForeignKey((new $model)->getName());
        $localKey   = $localKey ?: (new $model)->getPk();
        $trace      = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $relation   = Str::snake($trace[1]['function']);
        return new BelongsTo($this, $model, $foreignKey, $localKey, $relation);
    }

    /**
     * HAS MANY 关联定义
     * @return HasMany
     */
    public function hasMany($model, $foreignKey = '', $localKey = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        return new HasMany($this, $model, $foreignKey, $localKey);
    }

    /**
     * HAS MANY 远程关联定义
     * @return HasManyThrough
     */
    public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $throughPk = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $through    = $this->parseModel($through);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        $throughKey = $throughKey ?: $this->getForeignKey((new $through)->getName());
        $throughPk  = $throughPk ?: (new $through)->getPk();
        return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
    }

    /**
     * HAS ONE 远程关联定义
     * @return HasOneThrough
     */
    public function hasOneThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $throughPk = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $through    = $this->parseModel($through);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        $throughKey = $throughKey ?: $this->getForeignKey((new $through)->getName());
        $throughPk  = $throughPk ?: (new $through)->getPk();
        return new HasOneThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
    }

    /**
     * BELONGS TO MANY 关联定义
     * @return BelongsToMany
     */
    public function belongsToMany($model, $middle = '', $foreignKey = '', $localKey = '')
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $name       = Str::snake(class_basename($model));
        $middle     = $middle ?: Str::snake($this->name) . '_' . $name;
        $foreignKey = $foreignKey ?: $name . '_id';
        $localKey   = $localKey ?: $this->getForeignKey($this->name);
        return new BelongsToMany($this, $model, $middle, $foreignKey, $localKey);
    }

    /**
     * MORPH  One 关联定义
     * @return MorphOne
     */
    public function morphOne($model, $morph = null, $type = '')
    {
        // 记录当前关联信息
        $model = $this->parseModel($model);
        if (is_null($morph)) {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
            $morph = Str::snake($trace[1]['function']);
        }
        if (is_array($morph)) {
            list($morphType, $foreignKey) = $morph;
        } else {
            $morphType  = $morph . '_type';
            $foreignKey = $morph . '_id';
        }
        $type = $type ?: get_class($this);
        return new MorphOne($this, $model, $foreignKey, $morphType, $type);
    }

    /**
     * MORPH  MANY 关联定义
     * @return MorphMany
     */
    public function morphMany($model, $morph = null, $type = '')
    {
        // 记录当前关联信息
        $model = $this->parseModel($model);
        if (is_null($morph)) {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
            $morph = Str::snake($trace[1]['function']);
        }
        $type = $type ?: get_class($this);
        if (is_array($morph)) {
            list($morphType, $foreignKey) = $morph;
        } else {
            $morphType  = $morph . '_type';
            $foreignKey = $morph . '_id';
        }
        return new MorphMany($this, $model, $foreignKey, $morphType, $type);
    }

    /**
     * MORPH TO 关联定义
     * @return MorphTo
     */
    public function morphTo($morph = null, array $alias = [])
    {
        $trace    = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $relation = Str::snake($trace[1]['function']);
        if (is_null($morph)) {
            $morph = $relation;
        }
        // 记录当前关联信息
        if (is_array($morph)) {
            list($morphType, $foreignKey) = $morph;
        } else {
            $morphType  = $morph . '_type';
            $foreignKey = $morph . '_id';
        }
        return new MorphTo($this, $morphType, $foreignKey, $alias, $relation);
    }

    /**
     * MORPH TO MANY关联定义
     * @return MorphToMany
     */
    public function morphToMany($model, $middle, $morph = null, $localKey = null)
    {
        if (is_null($morph)) {
            $morph = $middle;
        }
        // 记录当前关联信息
        if (is_array($morph)) {
            list($morphType, $morphKey) = $morph;
        } else {
            $morphType = $morph . '_type';
            $morphKey  = $morph . '_id';
        }
        $model    = $this->parseModel($model);
        $name     = Str::snake(class_basename($model));
        $localKey = $localKey ?: $this->getForeignKey($name);
        return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $localKey);
    }

    /**
     * 解析模型的完整命名空间
     * @return string
     */
    protected function parseModel($model)
    {
        if (false === strpos($model, '\\')) {
            $path = explode('\\', static::class);
            array_pop($path);
            array_push($path, Str::studly($model));
            $model = implode('\\', $path);
        }
        return $model;
    }

    /**
     * 获取模型的默认外键名
     * @return string
     */
    protected function getForeignKey($name)
    {
        if (strpos($name, '\\')) {
            $name = class_basename($name);
        }
        return Str::snake($name) . '_id';
    }

    /**
     * 检查属性是否为关联属性 如果是则返回关联方法名
     * @return string|false
     */
    protected function isRelationAttr($attr)
    {
        $relation = Str::camel($attr);
        if ((method_exists($this, $relation) && !method_exists('think\Model', $relation)) || isset(static::$macro[static::class][$relation])) {
            return $relation;
        }
        return false;
    }

    /**
     * 智能获取关联模型数据
     * @return mixed
     */
    protected function getRelationData(Relation $modelRelation)
    {
        if ($this->parent && !$modelRelation->isSelfRelation() && get_class($this->parent) == get_class($modelRelation->getModel())) {
            return $this->parent;
        }
        // 获取关联数据
        return $modelRelation->getRelation();
    }

    /**
     * 关联数据自动写入检查
     * @return void
     */
    protected function checkAutoRelationWrite()
    {
        foreach ($this->together as $key => $name) {
            if (is_array($name)) {
                if (key($name) === 0) {
                    $this->relationWrite[$key] = [];
                    // 绑定关联属性
                    foreach ($name as $val) {
                        if (isset($this->data[$val])) {
                            $this->relationWrite[$key][$val] = $this->data[$val];
                        }
                    }
                } else {
                    // 直接传入关联数据
                    $this->relationWrite[$key] = $name;
                }
            } elseif (isset($this->relation[$name])) {
                $this->relationWrite[$name] = $this->relation[$name];
            } elseif (isset($this->data[$name])) {
                $this->relationWrite[$name] = $this->data[$name];
                unset($this->data[$name]);
            }
        }
    }

    /**
     * 自动关联数据更新（针对一对一关联）
     * @return void
     */
    protected function autoRelationUpdate()
    {
        foreach ($this->relationWrite as $name => $val) {
            if ($val instanceof Model) {
                $val->exists(true)->save();
            } else {
                $model = $this->getRelation($name, true);
                if ($model instanceof Model) {
                    $model->exists(true)->save($val);
                }
            }
        }
    }

    /**
     * 自动关联数据写入（针对一对一关联）
     * @return void
     */
    protected function autoRelationInsert()
    {
        foreach ($this->relationWrite as $name => $val) {
            $method = Str::camel($name);
            $this->$method()->save($val);
        }
    }

    /**
     * 自动关联数据删除（支持一对一及一对多关联）
     * @access protected
     * @param  bool $force 强制删除
     * @return void
     */
    protected function autoRelationDelete($force = false)
    {
        foreach ($this->relationWrite as $key => $name) {
            $name   = is_numeric($key) ? $name : $key;
            $result = $this->getRelation($name, true);
            if ($result instanceof Model) {
                $result->force($force)->delete();
            } elseif ($result instanceof Collection) {
                foreach ($result as $model) {
                    $model->force($force)->delete();
                }
            }
        }
    }

    /**
     * 移除当前模型的关联属性
     * @access public
     * @return $this
     */
    public function removeRelation()
    {
        $this->relation = [];
        return $this;
    }
}